package com.alibaba.spring.boot.rsocket.broker.route.impl;

import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.multimap.Multimap;
import org.eclipse.collections.api.multimap.bag.MutableBagMultimap;
import org.eclipse.collections.api.multimap.list.MutableListMultimap;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.multimap.list.AbstractMutableListMultimap;
import org.eclipse.collections.impl.multimap.list.FastListMultimap;
import org.eclipse.collections.impl.utility.Iterate;

import java.io.Externalizable;
import java.util.Collection;
import java.util.function.Predicate;

/**
 * FastListMultimap with value copies management
 *
 * @author leijuan
 */
public class FastListMultimap2<K, V> extends AbstractMutableListMultimap<K, V> implements Externalizable {
    private static final long serialVersionUID = 1L;

    // Default from FastList
    private static final int DEFAULT_CAPACITY = 1;

    private int initialListCapacity;

    public FastListMultimap2() {
        this.initialListCapacity = DEFAULT_CAPACITY;
    }

    public FastListMultimap2(int distinctKeys, int valuesPerKey) {
        super(Math.max(distinctKeys * 2, 16));
        if (distinctKeys < 0 || valuesPerKey < 0) {
            throw new IllegalArgumentException("Both arguments must be positive.");
        }
        this.initialListCapacity = valuesPerKey;
    }

    public FastListMultimap2(Multimap<? extends K, ? extends V> multimap) {
        this(
                multimap.keysView().size(),
                multimap instanceof FastListMultimap2
                        ? ((FastListMultimap2<?, ?>) multimap).initialListCapacity
                        : DEFAULT_CAPACITY);
        this.putAll(multimap);
    }

    public FastListMultimap2(Pair<K, V>... pairs) {
        super(pairs);
    }

    public FastListMultimap2(Iterable<Pair<K, V>> inputIterable) {
        super(inputIterable);
    }

    public static <K, V> FastListMultimap<K, V> newMultimap() {
        return new FastListMultimap<>();
    }

    public static <K, V> FastListMultimap<K, V> newMultimap(Multimap<? extends K, ? extends V> multimap) {
        return new FastListMultimap<>(multimap);
    }

    public static <K, V> FastListMultimap<K, V> newMultimap(Pair<K, V>... pairs) {
        return new FastListMultimap<>(pairs);
    }

    public static <K, V> FastListMultimap<K, V> newMultimap(Iterable<Pair<K, V>> inputIterable) {
        return new FastListMultimap<>(inputIterable);
    }

    @Override
    protected MutableMap<K, MutableList<V>> createMap() {
        return UnifiedMap.newMap();
    }

    @Override
    protected MutableMap<K, MutableList<V>> createMapWithKeyCount(int keyCount) {
        return UnifiedMap.newMap(keyCount);
    }

    @Override
    protected MutableList<V> createCollection() {
        return FastList.newList(this.initialListCapacity);
    }

    public void trimToSize() {
        for (Collection<V> collection : this.map.values()) {
            FastList<V> fastList = (FastList<V>) collection;
            fastList.trimToSize();
        }
    }

    @Override
    public FastListMultimap<K, V> newEmpty() {
        return new FastListMultimap<>();
    }

    @Override
    public MutableBagMultimap<V, K> flip() {
        return Iterate.flip(this);
    }

    @Override
    public FastListMultimap<K, V> selectKeysValues(Predicate2<? super K, ? super V> predicate) {
        return this.selectKeysValues(predicate, this.newEmpty());
    }

    @Override
    public FastListMultimap<K, V> rejectKeysValues(Predicate2<? super K, ? super V> predicate) {
        return this.rejectKeysValues(predicate, this.newEmpty());
    }

    @Override
    public MutableListMultimap<K, V> selectKeysMultiValues(Predicate2<? super K, ? super RichIterable<V>> predicate) {
        return this.selectKeysMultiValues(predicate, this.newEmpty());
    }

    @Override
    public MutableListMultimap<K, V> rejectKeysMultiValues(Predicate2<? super K, ? super RichIterable<V>> predicate) {
        return this.rejectKeysMultiValues(predicate, this.newEmpty());
    }

    public boolean removeAllSameValue(K key, V value) {
        MutableList<V> collection = this.map.get(key);
        if (collection == null) {
            return false;
        }
        int originSize = collection.size();
        boolean changed = collection.removeIf(Predicate.isEqual(value));
        if (changed) {
            this.totalSize = this.totalSize - (originSize - collection.size());
            if (collection.isEmpty()) {
                this.map.remove(key);
            }
        }
        return changed;
    }

    public boolean keepOnlyOne(K key, V value) {
        MutableList<V> collection = this.map.get(key);
        if (collection == null) {
            return false;
        }
        int originSize = collection.size();
        collection.removeIf(Predicate.isEqual(value));
        collection.add(value);
        this.totalSize = this.totalSize - (originSize - collection.size());
        return true;
    }

    public boolean putMultiCopies(K key, V value, int count) {
        MutableList<V> collection = this.map.getIfAbsentPutWith(key, this.createCollectionBlock(), this);
        for (int i = 0; i < count; i++) {
            collection.add(value);
        }
        this.totalSize = this.totalSize + count;
        return true;
    }

    public int getValueCount(K key, V value) {
        MutableList<V> collection = this.map.get(key);
        if (collection == null) {
            return 0;
        }
        return collection.count(v -> v.equals(value));
    }

}